/*
 * @Author: 0x9DEFA478
 * @Date: 2021-07-30 18:55:53
 * @LastEditTime: 2021-09-30 15:22:42
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "GUI_User.h"
#include "Peripheral.h"
#include "GUI_User_Page.h"


extern unsigned char GUI_User_OLED_Gram0[];
extern unsigned char GUI_User_OLED_Gram1[];
extern const unsigned char H0x9DEFA478_Logo[];


H_Graphics_Bitmap* volatile Bitmap_0x9DEFA478_Logo=NULL;
volatile int Button_Screen_FPS;


H_TS_Thread* Screen_Display_Thread_Handle;
GUI_User_DisplayThreadParam Screen_Display_Param;


static H_TS_MessageQueue* volatile GUI_User_delegate=NULL;
static volatile int GUI_User_IsRespondButton;
static int IsDisplay;

static void NextPageCallback(H_UI* h_ui,H_UI_Page* Old,H_UI_Page* New);
static void LastPageCallback(H_UI* h_ui,H_UI_Page* Old,H_UI_Page* New);
static void DisplayLogo(H_Graphics_Bitmap*);








static void Button_EventCallback(void* v,Huint32 Event){
  static int Button_B1_EventShield=0;
  static int Button_B2_EventShield=0;
  static int Button_B3_EventShield=0;
  static int Button_B4_EventShield=0;

  switch (Event&vH_Key_Event_KeyID){
    case vButton_B1:
      if(Button_B1_EventShield!=0){
        Button_B1_EventShield=0;
        return;
      }
      if(Event&vH_Key_Event_Up){
        //按键抬起事件
        if(IsDisplay!=0){
          H_UI_BackEvent(v);
        }
      }else if(Event&vH_Key_Event_Press){
        //按键按下事件

      }else if(Event&vH_Key_Event_LongPress){
        //长按事件

        Button_B1_EventShield=-1;
        
      }else{
      }
      break;

    case vButton_B2:
      if(Button_B2_EventShield!=0){
        Button_B2_EventShield=0;
        return;
      }
      if(Event&vH_Key_Event_Up){
        //按键抬起事件
        if(IsDisplay!=0){
          H_UI_EnterEvent(v);
        }
      }else if(Event&vH_Key_Event_Press){
        //按键按下事件
        
      }else if(Event&vH_Key_Event_LongPress){
        //长按事件

        Button_B2_EventShield=-1;
        
        if(IsDisplay==0){
          IsDisplay=-1;
        }else{
          IsDisplay=0;
        }
        H_TS_ThreadSleep(20);
        OLED_SetIsDisplay(Screen_OLED1,IsDisplay);
      }else{
      }
      break;

    case vButton_B3:
      if(Button_B3_EventShield!=0){
        Button_B3_EventShield=0;
        return;
      }
      if(Event&vH_Key_Event_Up){
        //按键抬起事件
        if(IsDisplay!=0){
          H_UI_SubEvent(v);
        }
      }else if(Event&vH_Key_Event_Press){
        //按键按下事件
        
      }else if(Event&vH_Key_Event_LongPress){
        //长按事件

        Button_B3_EventShield=-1;

        
        
      }else{
      }
      break;

    case vButton_B4:
      if(Button_B4_EventShield!=0){
        Button_B4_EventShield=0;
        return;
      }
      if(Event&vH_Key_Event_Up){
        //按键抬起事件
        if(IsDisplay!=0){
          H_UI_AddEvent(v);
        }
      }else if(Event&vH_Key_Event_Press){
        //按键按下事件
        
      }else if(Event&vH_Key_Event_LongPress){
        //长按事件

        Button_B4_EventShield=-1;

        
        
      }else{
      }
      break;
    
    default:
      break;
  }
}

//设置是否响应按键
void GUI_User_SetIsRespondButton(int IsRespondButton){
  GUI_User_IsRespondButton=IsRespondButton;
}

/**
 * @brief 向UI线程发起委托 该方法只能由线程调用
 * @param Callee 要执行的代码
 * @param v Callee的传入参数
 * @return 0:成功 其他:失败
 */
int Button_Screen_Thread_BeginInvoke(void (*Callee)(void*),void* v){
  if(GUI_User_delegate==NULL){
    return -1;
  }
  if(H_TS_MessageQueueSend(GUI_User_delegate,Callee,v)!=0){
    return -1;
  }
  return 0;
}

/**
 * @brief UI线程 (管理按键输入与屏幕显示)
 * @param v ...
 * @return 无
 */
int Button_Screen_Thread(void* v){
  void (*Callee)(void*);
  void* Callee_v;
  H_Graphics_Bitmap* Gram;
  H_Graphics_Bitmap Gram0;
  //H_Graphics_Bitmap Gram1;
  H_UI_Page* page;
  H_UI* h_ui;
  int t;
  int LastT;
  int FrameLastT;
  int FrameCnt;

  IsDisplay=-1;

  Gram0.Bitmap=GUI_User_OLED_Gram0;
  Gram0.Width=128;
  Gram0.Height=64;

  // Gram1.Bitmap=GUI_User_OLED_Gram1;
  // Gram1.Width=128;
  // Gram1.Height=64;

  FrameCnt=0;
  Button_Screen_FPS=0;


  

  //新建队列
  GUI_User_delegate=new_H_TS_MessageQueue(64);

  //初始化ui句柄
  h_ui=new_H_UI(MemoryMalloc_Malloc,MemoryMalloc_Free,NextPageCallback,LastPageCallback);
  if(h_ui==NULL){
    for(;;){
    }
  }

  //初始化按键
  GUI_User_IsRespondButton=-1;
  I_Button_Init(Button_EventCallback,h_ui);

  //初始化并显示Logo
  DisplayLogo(&Gram0);
  
  //初始化队列和刷屏线程
  Screen_Display_Param.RecvQueue=new_H_TS_MessageQueue(3);
  Screen_Display_Param.RecvDone=new_H_TS_Semaphore(1);
  if((Screen_Display_Param.RecvQueue==NULL)||(Screen_Display_Param.RecvDone==NULL)){
    for(;;){
    }
  }
  Screen_Display_Thread_Handle=H_TS_StartNewThread_HighPriority(Screen_Display_Thread,&Screen_Display_Param,512,H_TS_GetThisThread()->Priority,0);
  if(Screen_Display_Thread_Handle==NULL){
    for(;;){
    }
  }
  Screen_Display_Thread_Handle->ThreadName="Screen_Display_Thread";

  //初始化页面
  page=H_UI_StringItemPage_InitNewPage(h_ui,NULL,GUI_User_Page_Main_Init);
  page=H_UI_StringItemPage_InitNewPage(h_ui,page,GUI_User_Page_Screen_Init);
  page=H_UI_StringItemPage_InitNewPage(h_ui,page,GUI_User_Page_Save_Init);

  page=H_UI_StringItemPage_InitNewPage(h_ui,page,GUI_User_Page_Info_Init);

  LastT=Time_GetTimeMs();
  FrameLastT=LastT;

  Gram=&Gram0;
  for(;;){


    while(0==H_TS_MessageQueueTryReceive(GUI_User_delegate,(void**)&Callee,&Callee_v)){
      Callee(Callee_v);//执行委托
    }

    if((Time_GetTimeMs()-LastT)>999){
      LastT+=1000;

      Button_Screen_FPS=FrameCnt;
      FrameCnt=0;
      
    }

    if((Time_GetTimeMs()-FrameLastT)>5){
      FrameLastT+=6;

      if(GUI_User_IsRespondButton!=0){
        I_Button_Loop();
      }
      

      // if(Gram==&Gram0){
      //   Gram=&Gram1;
      // }else{
      //   Gram=&Gram0;
      // }
      GUI_User_Display(h_ui,Gram);
      
      H_TS_MessageQueueSend(Screen_Display_Param.RecvQueue,Gram,NULL);
      H_TS_SemaphoreTake(Screen_Display_Param.RecvDone);

      FrameCnt++;
    }else{
      H_TS_ThreadSleep(3);
    }
    

  }


}











static void NextPageCallback(H_UI* h_ui,H_UI_Page* Old,H_UI_Page* New){
  GUI_User_Move move;

  move.Direction=180;
  //move.Direction=90;
  move.X=0;
  move.Y=0;
  move.Width=128;
  move.Height=64;
  move.Speed=2;
  move.Process=0;
  GUI_User_SetMove(&move);

}

static void LastPageCallback(H_UI* h_ui,H_UI_Page* Old,H_UI_Page* New){
  GUI_User_Move move;

  move.Direction=0;
  //move.Direction=270;
  move.X=0;
  move.Y=0;
  move.Width=128;
  move.Height=64;
  move.Speed=2;
  move.Process=0;
  GUI_User_SetMove(&move);

}

static void DisplayLogo(H_Graphics_Bitmap* Gram){
  H_Graphics_Bitmap* b;
  H_Graphics_Area_Param* Param;

  b=MemoryMalloc_Malloc(sizeof(H_Graphics_Bitmap));
  if(b==NULL){
    for(;;){
      //内存申请失败
    }
  }
  b->Bitmap=(void*)H0x9DEFA478_Logo;
  b->Width=70;
  b->Height=40;
  Bitmap_0x9DEFA478_Logo=b;


  H_Graphics_L1Vertical_Fill(Gram,0,0,0,Gram->Width,Gram->Height);
  Param=MemoryMalloc_Malloc(sizeof(H_Graphics_Area_Param));
  if(Param==NULL){
    while(1){
    }
  }
  Param->S=Bitmap_0x9DEFA478_Logo;
  Param->D=Gram;
  Param->sX=0;
  Param->sY=0;
  Param->dX=29;
  Param->dY=12;
  Param->Width=Bitmap_0x9DEFA478_Logo->Width;
  Param->Height=Bitmap_0x9DEFA478_Logo->Height;
  H_Graphics_L1Vertical_CopyTo_L1Vertical(Param);
  OLED_Display(Screen_OLED1,Gram->Bitmap);
  MemoryMalloc_Free(Param);
  H_TS_ThreadSleep(1067);

}
